import { getSliderValue } from "./displayInfor.js";
export function stopByBuyOrSell() {
  const btn = document.querySelector(".trading_sub > .mantine-1v71nuo");
  console.log(btn, "btn");
  if (btn) {
    const originalHandler = btn.onclick;
    if (originalHandler) {
      btn.onclick = null;
    }
    btn.addEventListener("click", async (event) => {
      const sliderValue = getSliderValue();
      const params = getStopByConValue();
      const iocValue = getIocStopValue();
      console.log(sliderValue, params, iocValue, "sliderValue");
      console.log("买入按钮被点击");
      event.preventDefault();
      console.log("执行自定义逻辑");
      // chrome.runtime.sendMessage(
      //   {
      //     action: "interceptRequest",
      //     url: "https://www.gate.io/apiw/v2/unified/futures/usdt/contracts",
      //   },
      //   (response) => {
      //     if (chrome.runtime.lastError) {
      //       console.error("消息发送失败：", chrome.runtime.lastError.message);
      //     } else {
      //       console.log("收到响应：", response);
      //     }
      //   }
      // );
    });
  } else {
    console.log("未找到.trading_sub > .mantine-1v71nuo买入按钮节点");
  }
}

function getStopByConValue() {
  const entrustePriceGroup = document.querySelectorAll(
    ".mantine-1i52ecs > input"
  );
  const params = {
    price: entrustePriceGroup[0].value, // 触发价格
    entrustePrice: entrustePriceGroup[1].value, // 委托价格
    amount: entrustePriceGroup[2].value, // 数量
    turnoverValue: entrustePriceGroup[3].value, // 交易额
  };
  const topShortInputGroup = document.querySelectorAll(
    ".mantine-dcm9q0 > .mantine-wz61vw"
  );
  console.log(topShortInputGroup, "topShortInputGroup");
  params.leadTo = topShortInputGroup[0].value;
  params.days = topShortInputGroup[1].value;
  console.log(params, "params");
  return params;
}

function getIocStopValue() {
  const checkbox = document.querySelector(
    ".mantine-muwcf3 > .mantine-xsvmgd > .mantine-11lhrkx > .mantine-14u1gnw"
  );
  console.log(checkbox, "checkboxGroup");
  return checkbox.checked;
}
